package com.ytc.www.service;

import com.ytc.www.dto.UserDTO;
import com.ytc.www.dto.PageDTO;
import com.ytc.www.dto.ResponseBox;
import com.ytc.www.entity.EducationUser;
import com.ytc.www.sqlmapping.EducationUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * online_education
 * com.ytc.www.service
 *
 * @author 姚自豪
 * @create 2022-03-23 12:33
 */
@Service
public class UserService {
    @Autowired
    EducationUserMapper mapper;


    public ResponseBox insert(EducationUser educationUser){
        ResponseBox responseBox = new ResponseBox();
        try {
            mapper.insert(educationUser);
            responseBox.setResponseCode(200);
            responseBox.setMessage("操作成功");
        } catch (Exception e) {
            responseBox.setResponseCode(500);
            responseBox.setMessage(e.getMessage());
        }
        return responseBox;
    }

    public ResponseBox delete(int userId){
        ResponseBox responseBox = new ResponseBox();
        try {
            mapper.deleteByPrimaryKey(userId);
            responseBox.setResponseCode(200);
            responseBox.setMessage("操作成功");
        } catch (Exception e) {
            responseBox.setResponseCode(500);
            responseBox.setMessage(e.getMessage());
        }
        return responseBox;
    }

    public ResponseBox update(EducationUser educationUser){
        ResponseBox responseBox = new ResponseBox();
        try {
            mapper.updateByPrimaryKey(educationUser);
            responseBox.setResponseCode(200);
            responseBox.setMessage("操作成功");
        } catch (Exception e) {
            responseBox.setResponseCode(500);
            responseBox.setMessage(e.getMessage());
        }
        return responseBox;
    }

    public ResponseBox queryAll( UserDTO userDTO,PageDTO pageDTO){
        ResponseBox<PageDTO> responseBox = new ResponseBox();
        try{
            List<EducationUser> users = mapper.selectAll(userDTO,(pageDTO.getGotoPage()-1)*pageDTO.getPageSize(),pageDTO.getPageSize());
            int getAllPageRecordCount = mapper.selectUserCount(userDTO);
            int currentPageRecordCount=  users.size();
            pageDTO.setGetAllPageRecordCount(getAllPageRecordCount);
            pageDTO.setGetCurrentPageRecordCount(currentPageRecordCount);
            pageDTO.setPageData(users);
            responseBox.setResponseCode(200);
            responseBox.setMessage("操作成功");
            responseBox.setData(pageDTO);
        } catch (Exception e){
            e.printStackTrace();
            responseBox.setResponseCode(500);
            responseBox.setMessage(e.getMessage());
        }
        return responseBox;
    }

    public ResponseBox queryByUserId(int userId){
        ResponseBox<EducationUser> responseBox = new ResponseBox<>();
        try {
            EducationUser educationUser = mapper.selectByPrimaryKey(userId);
            responseBox.setResponseCode(200);
            responseBox.setMessage("操作成功");
            responseBox.setData(educationUser);
        } catch (Exception e) {
            responseBox.setResponseCode(200);
            responseBox.setMessage(e.getMessage());
        }
        return responseBox;
    }


    public ResponseBox pagedata(PageDTO pageDTO) {
        ResponseBox responseBox = new ResponseBox<>();
        try {
            List<EducationUser> users= mapper.pagedata((pageDTO.getGotoPage()-1)*pageDTO.getPageSize(),pageDTO.getPageSize());
            pageDTO.setPageData(users);
            responseBox.setResponseCode(200);
            responseBox.setMessage("操作成功");
            responseBox.setData(pageDTO);
        } catch (Exception e) {
            responseBox.setResponseCode(200);
            responseBox.setMessage(e.getMessage());
        }
        return responseBox;
    }
}

